package hongcaosp.app.android.user.settings.invite;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.blackchopper.loginshare.impl.LoginShare;
import com.blackchopper.loginshare.interfaces.OnLoginshareListener;
import com.blackchopper.loginshare.model.Type;
import com.blackchopper.loginshare.model.WechatMessageBody;

import hongcaosp.app.android.R;
import hongcaosp.app.android.contact.ListActivity;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.modle.DataCallBack;
import hongcaosp.app.android.modle.bean.InviteRewardWrap;
import hongcaosp.app.android.modle.impl.OtherModelImpl;
import xlj.lib.android.base.component.BaseActivity;
import xlj.lib.android.base.toastcompat.ToastManager;

/**
 * 邀请有礼
 */
public class InviteActivity extends BaseActivity implements OnLoginshareListener {

    public static void launcher(Context context) {
        context.startActivity(new Intent(context,InviteActivity.class));
    }

    private TextView tv_code,tv_money;

    private LoginShare loginShare;
    private String code;

    @Override
    protected void initData(Intent intent) {

        code = UserToken.getDefault().getUser().getInvitationCode();
        tv_code.setText("我的邀请码：" + code);

        new OtherModelImpl().inviteReward(new DataCallBack<InviteRewardWrap>() {
            @Override
            public void onGetData(InviteRewardWrap data) {
                super.onGetData(data);
                if (data != null && data.getInviteReward() != null) {
                    String temp = "";
                    try {
                        Double d = Double.valueOf(data.getInviteReward().getDicValue());
                        if (d > 0) {
                            temp = data.getInviteReward().getDicValue();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (TextUtils.isEmpty(temp)) {
                        tv_money.setVisibility(View.GONE);
                        return;
                    }
                    tv_money.setText("会员特权：邀请好友注册并充值会员，永久返利" + temp +"%");
                    Drawable d = getResources().getDrawable(R.drawable.icon_mark);
                    d.setBounds(0,0,d.getMinimumHeight(),d.getMinimumWidth());
                    tv_money.setCompoundDrawables(null,null,d,null);
                    tv_money.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            startActivity(new Intent(InviteActivity.this,InviteDescActivity.class));
                        }
                    });
                }
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                ToastManager.getInstance().showToast(errorMessage);
            }
        });
    }

    @Override
    protected void initView() {
        loginShare = new LoginShare(this);
        loginShare.register(this);

        findViewById(R.id.tv_record).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ListActivity.toActivity(InviteActivity.this,ListActivity.LIST_TYPE_INVITE);
            }
        });

        findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        tv_code = findViewById(R.id.tv_code);
        tv_money = findViewById(R.id.tv_money);
        findViewById(R.id.btn_copy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager clipboardManager = (ClipboardManager) InviteActivity.this.getSystemService(Context.CLIPBOARD_SERVICE);
                if (clipboardManager != null) {
                    clipboardManager.setPrimaryClip(ClipData.newPlainText("text",code));
                    ToastManager.getInstance().showToast("复制成功");
                }
            }
        });
        findViewById(R.id.ll_share_wechat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                share(WechatMessageBody.WECHAT);
            }
        });
        findViewById(R.id.ll_share_timeline).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                share(WechatMessageBody.TIMELINE);
            }
        });
    }

    private void share(int type) {
        if (TextUtils.isEmpty(code)) {
            ToastManager.getInstance().showToast("请先复制邀请码噢");
            return;
        }
        WechatMessageBody wechatMessageBody = new WechatMessageBody();
        wechatMessageBody.type = type;
        wechatMessageBody.webpageUrl("https://a.app.qq.com/o/simple.jsp?pkgname=hongcaosp.app.android&fromcase=40002");
        wechatMessageBody.msgType = WechatMessageBody.MSG_WEB;
//        wechatMessageBody.text("我发现一个好玩的app，每天都有现金大红包抢，大家都来玩吧。");
        wechatMessageBody.description("邀请码：" + code);    //实测，没有description到了发送给朋友界面，点击某个人无反应。。
        wechatMessageBody.title("我发现一个好玩的app，每天都有现金大红包抢，大家都来玩吧。");

        loginShare.launchWechatShare(wechatMessageBody);
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_invite;
    }

    @Override
    protected void onStop() {
        super.onStop();
        loginShare.unRegister();
    }

    @Override
    public void onLoginSuccess(Type type, String code, String info) {

    }

    @Override
    public void onLoginCancel(Type type) {

    }

    @Override
    public void onLoginError(Type type, int errorCode) {

    }

    @Override
    public void onShareSuccess(Type type) {
        ToastManager.getInstance().showToast("分享成功");
    }

    @Override
    public void onShareCancel(Type type) {
        ToastManager.getInstance().showToast("分享取消");
    }

    @Override
    public void onShareError(Type type, int code) {
        ToastManager.getInstance().showToast("分享失败");
    }
}
